package org.tangram.component.xml;

import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.stream.InputNode;
import org.simpleframework.xml.stream.NodeMap;
import org.simpleframework.xml.stream.Position;

class ConfigInputNode implements InputNode {

	private String name;
	private String value;
	private boolean root;
	private boolean element;
	private boolean empty;
	private String prefix;
	private String reference;

	private Position position;
	private InputNode parent;
	private Object source;

	private int index = -1;

	private List<InputNode> children = new ArrayList<InputNode>();

	private NodeMap<InputNode> attributes;

	private static class ConfigNodePosition implements Position {

		private int line;
		private String str;

		ConfigNodePosition(Position position) {
			this.line = position.getLine();
			this.str = position.toString();
		}

		public int getLine() {
			return line;
		}

		public String toString() {
			return str;
		}

	}

	public ConfigInputNode(InputNode node) throws Exception {
		this(node, null);
	}

	public ConfigInputNode(InputNode node, InputNode parent) throws Exception {
		this.parent = parent;

		this.name = node.getName();
		this.value = node.getValue();
		this.root = node.isRoot();
		this.element = node.isElement();
		this.empty = node.isEmpty();
		this.prefix = node.getPrefix();
		this.reference = node.getReference();
		this.position = new ConfigNodePosition(node.getPosition());
		this.source = node.getSource();
		this.attributes = node.getAttributes();

		for (InputNode child = node.getNext(); child != null; child = node
				.getNext()) {
			this.children.add(new ConfigInputNode(child, this));
		}
	}

	public String getName() {
		return name;
	}

	public String getValue() throws Exception {
		return value;
	}

	public boolean isRoot() {
		return root;
	}

	public boolean isElement() {
		return element;
	}

	public String getPrefix() {
		return prefix;
	}

	public String getReference() {
		return reference;
	}

	public Position getPosition() {
		return position;
	}

	public InputNode getAttribute(String name) {
		return attributes.get(name);
	}

	public NodeMap<InputNode> getAttributes() {
		return attributes;
	}

	public InputNode getParent() {
		return parent;
	}

	public Object getSource() {
		return source;
	}

	public InputNode getNext() throws Exception {
		index++;
		if (index < children.size()) {
			return children.get(index);
		} else
			return null;
	}

	public InputNode getNext(String name) throws Exception {
		return null;
	}

	public void skip() throws Exception {
		index = children.size();
	}

	public boolean isEmpty() throws Exception {
		return empty;
	}

}
